<template>
	<el-card class="layout-query">
		<el-form :inline="true">
			<el-form-item label="阶段状态">
				<el-select v-model="state.queryParams.stageType" placeholder="请选择" style="width: 100%" clearable>
					<el-option v-for="item in selectlist" :key="item.code" :label="item.name" :value="item.code"></el-option>
				</el-select>
			</el-form-item>

			<el-form-item>
				<el-button icon="Search" type="primary" @click="handleSearch">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button icon="RefreshRight" @click="reset">重置</el-button>
			</el-form-item>
		</el-form>
	</el-card>

	<el-card>
		<el-form :inline="true" style="text-align: right">
			<el-form-item>
				<el-button type="primary" @click="addOrUpdateHandle(true, {}, 1)">新增</el-button>
			</el-form-item>
		</el-form>

		<el-table
			v-loading="loading"
			:data="datalist"
			stripe
			element-loading-background="rgba(0, 0, 0, 0.5)"
			element-loading-text="加载中..."
			header-row-class-name="table-header"
		>
			<el-table-column type="index" label="序号" header-align="center" align="center" width="60"></el-table-column>
			<el-table-column label="附件名称" align="center" prop="definitionName" />
			<el-table-column label="附件代码" align="center" prop="businessCode"> </el-table-column>
			<el-table-column label="附件类型" align="center" prop="suffixType"> </el-table-column>
			<el-table-column label="操作人" align="center" prop="processor"> </el-table-column>
			<el-table-column label="创建时间" align="center" prop="createTime"> </el-table-column>
			<el-table-column label="修改时间" align="center" prop="updateTime"> </el-table-column>
			<el-table-column label="评审阶段" align="center" prop="businessStageValue"> </el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="110">
				<template #default="scope">
					<el-icon :size="16" color="#1491FF" title="编辑" style="cursor: pointer; margin: 0 6px" @click="addOrUpdateHandle(false, scope.row, 1)">
						<Edit />
					</el-icon>
					<el-icon :size="16" color="#fe6360" title="删除" style="cursor: pointer; margin: 0 6px" @click="deleteBatchHandle(scope.row.id)">
						<Delete />
					</el-icon>
				</template>
			</el-table-column>
		</el-table>

		<el-pagination
			:current-page="pageInfo.pageIndex"
			:page-size="pageInfo.pageSize"
			:total="pageInfo.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="handleSizeChange"
			@current-change="handleCurrentChange"
		>
		</el-pagination>
	</el-card>
	<AddDialog ref="addOrUpdateRef" @refresh-data-list="getDataList" />
</template>
<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { useDictionaryStore } from '@/store/modules/dictionary'
import { listApi, deletetApi } from '@/api/system/fifle'
import { ElMessage, ElMessageBox } from 'element-plus'
// import { router } from '@/router'
import AddDialog from './components/addDialog.vue'
const dictionaryStore = useDictionaryStore()

const loading = ref(false)
const selectlist = dictionaryStore.getDictionaryByCode('T0021')
const datalist = ref([])
const pageInfo = ref({ pageIndex: 1, pageSize: 10, total: 0 })

const state = reactive({
	queryParams: {
		stageType: ''
	}
})
onMounted(() => {
	getDataList()
})
const getDataList = () => {
	loading.value = true
	const data = { pageIndex: pageInfo.value.pageIndex, pageSize: pageInfo.value.pageSize, stageType: state.queryParams.stageType }
	listApi(1, data).then(res => {
		loading.value = false
		datalist.value = res.data.list || []
		pageInfo.value.total = res.data.total || 0
	})
}

const handleSizeChange = (val: number) => {
	pageInfo.value.pageIndex = 1
	pageInfo.value.pageSize = val
	datalist.value = []
	getDataList()
}
const handleCurrentChange = (val: number) => {
	pageInfo.value.pageIndex = val
	datalist.value = []
	getDataList()
}

const handleSearch = () => {
	pageInfo.value.pageSize = 10
	pageInfo.value.pageIndex = 1
	getDataList()
}
const deleteBatchHandle = (id: String | Number) => {
	ElMessageBox.confirm('是否删除?', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			delMethods(id)
		})
		.catch(() => {
			ElMessage({
				type: 'info',
				message: '删除失败'
			})
		})
}

const delMethods = async (id: String | Number) => {
	await deletetApi({ id: id }).then(res => {
		ElMessage.success('删除成功')
		reset()
	})
}

const reset = () => {
	pageInfo.value.pageSize = 10
	pageInfo.value.pageIndex = 1
	datalist.value = []
	state.queryParams.stageType = ''
	pageInfo.value.total = 0
	getDataList()
}

// 新增编辑
const addOrUpdateRef = ref()
const addOrUpdateHandle = (isUpdate: Boolean, row: any, type: Number) => {
	// type  0：标准项目  1：前瞻性项目
	addOrUpdateRef.value.init(isUpdate, row, type)
}
</script>

<style lang="scss" scoped></style>
